﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using DHAA.Areas.Administrator.Models;

namespace DHAA.Global
{





    public class GlobalController
    {
        private SqlConnection sqlConnection { get; set; }
    
        public SqlConnection getConnection()
        {
            try
            {
                if (sqlConnection == null)
                {
                    sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DHAAConnectionString"].ToString());
                }
                if(sqlConnection.State != ConnectionState.Open)
                {
                    sqlConnection.Open(); 
                }
                return sqlConnection;
            }
            catch (Exception)
            {
                return null;

            }

        }

       private int count = 0;
       public string ListToTree(List<CategoryModels> catalist, int parrent)
       {
           if(parrent == 23)
           {
               int stop = 32;
           }



           string htmlCode;
           if(count == 0)
           {
                htmlCode = "<UL class=\"dha-vmenu\">";
           }
           else
           {
               htmlCode = "<UL>";
           }
           count++;
           var cat = catalist.Select(c => c).Where(c => c.ParentID == parrent).ToList();
           if(cat.Count == 1)
           {
               htmlCode += "<li><a href=\"#\"><span class=\"l\"></span><span class=\"r\"></span><span class=\"t\">" + cat[0].CategoryName.ToString() + "</span></a></li>";
        }
           if (cat.Count > 1)
           {
               foreach (CategoryModels t in cat)
               {
                   htmlCode +=
                       "<li><a href=\"#\"><span class=\"l\"></span><span class=\"r\"></span><span class=\"t\">" +
                       t.CategoryName + "</span></a>";
                   htmlCode += ListToTree(catalist, t.CategoryID);
                   htmlCode += "</LI>";
               }
           }

           htmlCode += "</UL>";
           return htmlCode;
       }


        public List<CategoryModels> RenderCategory(List<CategoryModels> list)
        {
            var treeCat = new List<CategoryModels>();
            foreach (var cat in list)
            {
                string b = "";
                string e = "";
                 for (int j = 0; cat.Level > j; j++)
                {
                    b = "<li>";
                    e = "</li>";
                }

                 for (int j = 1; cat.Level == j; j++)
                 {
                     b = "<li ID =\"E\">";
                     e = "</li>";
                 }
//for (int l = 0; l - cat.Level == cat.Level; l++)
//                {
//                    b = "</ul>---";
//                    e += "";
//                }
               
//                for (int k = 0; cat.Level - k < 1; k--)
//                {
//                    b += "</ul>  <ul><li>";
//                    e += "</li><ul>";
//                    //break;
//                }

                
                //int i = 0;
                //if(cat.Level-i == 1)
                //{
                  
                //}
                treeCat.Add(new CategoryModels
                                {
                                    CategoryID = cat.CategoryID,CategoryName = b+cat.CategoryName+e,Description = cat.Description, Level = cat.Level, ParentID = cat.ParentID
                                    
                                });

            }

            return treeCat;
            //  if ($parent_list){
            //foreach ($parent_list as $value)
            //{
            //    $str = "";
            //    for ($i = 1; $i < $value['level']; $i++) $str .= "---";
            //    $parentArr[$value['id']] = $str." ".$value['name'];

        }
          
   

        public string password2md5(string data)
        {

            var MD5 = new MD5CryptoServiceProvider();

            byte[] b = System.Text.Encoding.UTF8.GetBytes(data);

            b = MD5.ComputeHash(b);

            var s = new StringBuilder();

            foreach (byte p in b)
            {

                s.Append(p.ToString("x").ToLower());

            }

            return s.ToString();

        }

    }
}

